<template>
  <div class="app-container">
    <div class="filter-container">
      <!-- <el-input v-model="listQuery.sysLabel" placeholder="配置名称关键字" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button> -->
      <!-- <input type="text" :style="{color: testcolor }"> -->
      <el-button class="filter-item" style="margin-left: 10px;" type="success" icon="el-icon-plus" @click="handleCreate">
        新增
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="tableData"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="编号" prop="id" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="说明" class-name="status-col" width="200">
        <template slot-scope="{row}">
          {{ row.comment }}
        </template>
      </el-table-column>
      <el-table-column label="链接" min-width="200" align="center">
        <template slot-scope="{row}">
          <span>{{ row.link }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" min-width="200" align="center">
        <template slot-scope="{row}">
          <span>{{ row.gmtDatetime }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.current"
      :limit.sync="listQuery.size"
      @pagination="apkList"
    />

    <el-dialog :title="textMap[dialogStatus]" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="loan" label-position="left" label-width="90px" style="width: 400px; margin-left:50px;">


        <el-form-item label="App文件" prop="appName">
          <el-upload
            ref="upload"
            action="modules/appfile/uploadapk"
            :limit="limitNum"
            :auto-upload="false"
            accept=".apk"
            :before-upload="beforeUploadFile"
            :on-exceed="exceedFile"
            :on-success="handleSuccess"
            :on-error="handleError"
            :file-list="fileList"
            :http-request="uploadSectionFile"
          >
            <el-button size="small" plain>选择文件</el-button>
            <div slot="tip" class="el-upload__tip">
              只能上传apk文件
            </div>
          </el-upload>
        </el-form-item>
        <el-form-item label="说明文本">
          <el-input v-model="comment" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">
          取消
        </el-button>
        <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">
          上传
        </el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogPvVisible" title="Reading statistics">
      <el-table :data="pvData" border fit highlight-current-row style="width: 100%">
        <el-table-column prop="key" label="Channel" />
        <el-table-column prop="pv" label="Pv" />
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogPvVisible = false">Confirm</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { apkList, apkupload } from '@/api/sys'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'Sysapkupload',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      testcolor: 'red',
      tableData: [],
      total: 0,
      tableKey: 0,
      listQuery: {
        current: 1,
        size: 10,
        sysLabel: ''
      },
      loan: {
        id: undefined,
        appName: new Date(),
        h5: '',
        slogen: '',
        applyCount: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改',
        create: '新增'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        appName: [{ required: true, message: '名称必填', trigger: 'blur' }],
        h5: [{ required: true, message: 'h5地址必填', trigger: 'blur' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }]
      },
      downloadLoading: false,
      listLoading: false,
      limitNum: 1,
      fileList: [],
      comment: ''
    }
  },
  created() {
    this.apkList(this.listQuery)
  },
  methods: {
    // 获取列表
    async apkList(listQuery) {
      const res = await apkList(this.listQuery)
      console.log(res)
      this.tableData = res.data.data.records
      this.total = res.data.data.total
    },
    handleFilter() {
      this.listQuery.current = 1

      this.apkList(this.listQuery)
    },
    cancel() {
      this.dialogFormVisible = false
      this.apkList()
    },
    resetTemp() {
      this.loan = {
        id: undefined,
        username: '',
        gmtDatetime: '',
        state: 0
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      // 刷新表单数据验证提示
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    beforeUploadFile(file) {
      // console.log(file)
      const extension = file.name.substring(file.name.lastIndexOf('.') + 1)
      if (extension !== 'apk') {
        this.$notify.warning({
          title: '警告',
          message: `只能上传apk文件`
        })
      }
    },
    // 文件超出个数限制时的钩子
    exceedFile(files, fileList) {
      this.$notify.warning({
        title: '警告',
        message: `只能选择 ${
          this.limitNum
        } 个文件，当前共选择了 ${files.length + fileList.length} 个`
      })
    },
    // 文件上传成功时的钩子
    handleSuccess(res, file, fileList) {
      console.log(res)
      // this.formData.url = res.data  //服务器返回的文件地址
      this.$message({
        message: '文件上传成功',
        type: 'success'
      })
      this.$refs.upload.clearFiles()// 清楚上次上传记录
    },
    // 文件上传失败时的钩子
    handleError(err, file, fileList) {
      this.$message.error(err.msg)
    },
    submitUpload() {
      this.$refs.upload.submit()
    },
    uploadSectionFile(param) {
      var formData = new FormData()
      formData.append('file', param.file)
      formData.append('comment', this.comment)
      apkupload(formData).then((res) => {
        console.log(res)
        if (res.data.code === 200) {
          this.$notify({
            title: 'Success',
            message: 'Upload Successfully',
            type: 'success',
            duration: 2000
          })
          this.apkList()
        } else {
          this.$notify({
            title: 'fail',
            message: 'Upload fail',
            type: 'fail',
            duration: 2000
          })
        }
      })
      this.dialogFormVisible = false
    },
    getSortClass: function(key) {
      const sort = this.listQuery.sort
      return sort === `+${key}` ? 'ascending' : 'descending'
    }
  }
}
</script>

<style scoped>
.pic{
  width: 150px;
  margin-top: 10px;
  margin-left: 10px;
}
.ok{
  margin-left: 10px;
  margin-top: 10px;
  font-weight: 5 00;
  color:rgb(35, 248, 124)
}
.fail{
  margin-left: 10px;
  margin-top: 10px;
  font-weight: 5 00;
  color: rgb(240, 174, 76);
}
.info{
  margin-left: 10px;
  margin-top: 10px;
  font-weight: 5 00;
}
.info-name{
  margin-left: 50px;
  margin-top: 10px;
  color:rgb(172, 176, 177);
}
.title{
  margin-top: 10px;
  margin-left: 10px;
  font-size: 20px;
  font-weight: 400;
}
.box{
  margin-top: 10px;
  width: 100%;
  border: solid rgb(138, 219, 230) 2px;
}
.nick{
  font-size: 30px;
  font-weight: 600;
}
.link-top {
  margin-left: 10px;
  margin-top: 10px;
  width: 100%;
  height: 2px;
  border-top: solid rgb(138, 219, 230) 2px;
}
.link-title {
  margin-left: 10px;
  margin-top: 10px;
  width: 80px;
  height: 2px;
  border-top: solid rgb(172, 176, 177) 2px;
}
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
